define(function(require, exports, module) {
    "use strict"; 
     var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;
        $(".js_dcodetype:checked").length>0&&(function(){
        	var type=$(".js_dcodetype:checked").data("type");
        	$(".js_dcodetype_input").addClass('hide');
    		$(".js_"+type).each(function(){
    			var $this=$(this);
    			$this.removeClass('hide');
    			if($this.find("img").length>0&&!$this.find("img").attr("src")){
    				$this.addClass('hide');
    			}
    		});
        	$(".js_dcode_tips").addClass('hide');
        	$(".js_dcode_tips_"+type).removeClass('hide');
        })();
		$(document).on("keyup", ".js_pay_input", function() {
			var $this = $(this);
			$this.val($this.val().trim());
			$this.blur();
			$this.focus();
		});
        $(document).on("click",".js_dcodetype",function(){
        	var $this=$(this),checked=$this.prop("checked"),type=$this.data("type");
        	if(checked){
        		$(".js_dcodetype_input").addClass('hide');
        		$(".js_"+type).each(function(){
        			var $this=$(this);
        			$this.removeClass('hide');
        			if($this.find("img").length>0&&!$this.find("img").attr("src")){
        				$this.addClass('hide');
        			}
        		});
        		$(".js_dcode_tips").addClass('hide');
        		$(".js_dcode_tips_"+type).removeClass('hide');
        	}
        });
        
        var activityinit = function(custom) {
	        method.get(window.dcode_setting.dcodelabel_path, function(data) {
	            if (config.issucceed(data)) {
	                $activitylabel.html(template("activitylabel_template", data.data));
	                $(".js_activitylabel_list li").each(function() {
	                    var $this = $(this),
	                        $ul = $this.closest("ul"),
	                        id = $ul.attr("id"),
	                        text = $ul.find(".active input[type='radio']:checked").data("title");
	                    $("#label_" + id).html(text);
	                });
	            }else{
	                config.msg.error(data.message);
	            }
	           
	        }, config.lang.activitylabelError);
	        custom.customInput();
	        $(document).on("click", ".js_activitylabel_list .js_custom_btn", function() {
	            $(this).parents(".js_custom_list").prev(".js_custom_list").removeClass("hide");
	            $(this).parents(".js_custom_list").addClass("hide");
	            var $p = $(this).parents(".custom-select");
	            $p.scrollTop($p.get(0).scrollHeight - 0 + 60);
	        });
	        $(document).on("click", ".js_activitylabel_list .js_custom_list_save", function() {
	            var input = $(this).parents(".js_custom_list").find(".js_custom_input"),
	                name = $(this).data("name"),
	                tdata = custom.customdata(input);
	            if (custom.customValvali(input) && input.val()) {
	                if (custom.getValExist(input.val(), tdata.id)) {
	                    config.msg.info("已经存在的标签");
	                } else {
	                    custom.addVal(tdata, name, input, false, $(this),window.dcode_setting.custom_dcodevalue_path);
	                }

	            }
	            if (!input.val()) {
	                config.msg.error("标签为空！");
	            }
	        });
	        $(document).on("click", ".js_activitylabel_list .js_custom_list_cancel", function() {
	            $(this).parents(".js_custom_list").next(".js_custom_list").removeClass("hide");
	            $(this).parents(".js_custom_list").addClass("hide");
	        });
       
    	}
		var Custom = function() {

		}
	    Custom.prototype.customInput = function() {
	        var _this = this;
	        $(document).on("keyup", ".js_custom_input", function() {
	            var len = _this.getInputLen($(this)),limit=$(this).data("limit"),
	                limitmin = limit[0] - 0,
	                limitmax = limit[1] - 0,
	                $limit = $(this).next(".js_limit");
	            $limit.find("em").html(Math.ceil(len / 2));
	            if (len > limitmax * 2 || len<limitmin*2) {
	                $limit.addClass('error');
	            } else {
	                $limit.removeClass('error');
	            }
	        });
	    }
	    Custom.prototype.getInputLen = function(input) {
	        return input.val().replace(/[^\x00-\xff]/g, "xx").length;
	    }
	    Custom.prototype.customdata = function(input) {
	        return {
	            "id": input.data("id"),
	            "val": input.val()
	        }
	    }
	    Custom.prototype.customValvali = function(input) {
	        var flag = true,
	            limit = input.data("limit"),
	            limitmin = limit[0] - 0,
	            limitmax = limit[1] - 0,
	            len = this.getInputLen(input);
	        if (len<=limitmax * 2 && len>=limitmin * 2) {
	            flag = true;
	        } else {
	            flag = false;
	        }
	        return flag;

	    }
	    Custom.prototype.getValExist = function(val,id) {
	        var arr = [],
	            flag, isexist;
	        $("#" + id).find("input[type='radio']").each(function() {
	            if ($(this).val()) {
	                arr.push($(this).data("title"));
	            }
	        });
	        isexist = $.inArray(val, arr);
	        flag = isexist < 0 ? false : true;
	        return flag;
	    }
	    Custom.prototype.addVal = function(tdata, name, input, isattr, button,path) {
	        var _this = this;
	        button.data("loadingText", '<i class="fa fa-spinner fa-spin"></i>提交中');
	        button.button('loading');
	        $.ajax(path, {
	            type: "post",
	            data: tdata,
	            dataType: "json"
	        }).done(function(d) {
	            button.button('reset');
	            if (config.issucceed(d)) {
	                var data=d.data;
	                d.data.checked = true;
	                d.data.name = name;
	                _this.result(d.data, input, isattr);
	            } else {
	                config.msg.error(d.message);
	            }

	        }).fail(function() {
	            button.button('reset');
	            config.msg.info(config.lang.exception)
	        });
	    }
	    Custom.prototype.result = function(data, input, isattr) {
	        var $target = $("#" + data.id).find(".js_custom_list:first");
	        $("#" + data.id).find("li").removeClass("active");
	        $("#" + data.id).find("li input[type='radio']").removeAttr("checked");
	        $(template("customval_tem", data)).insertBefore($target);
	        $("#label_" + data.id).html(data.title);
	        $("#label_" + data.id).removeClass('error');
	        $(document).trigger('click');
	        input.val("");
	        input.next(".js_limit").find("em").html(0);
	        $target.addClass('hide');
	        $target.next(".js_custom_list").removeClass('hide');
	        $("#" + data.id).find(".js_search_result").addClass('hide');
	        config.msg.info(config.lang.saveSuccess);
	    }
	    var $activitylabel = $('[data-toggle="dcodelabel"]');
	    $activitylabel.length>0&&activityinit(new Custom());
		$activitylabel.length>0&&$(document).on("click","button[type='submit']",function(){
	        var $selected=$activitylabel.find(".js_activitylabel_list input[type='radio']:checked");
	        if($selected.val()==undefined||$selected.val()==""){
	            config.msg.error("请选择二维码标签");
	            return false;
	        }
    	});
		$("button.cancleLogo").click(function () {
            $(".addlogo[data-toggle='selectimg']").removeClass('hide');
            var $logo = $("input[name='logo']");
            $(".js_logo").attr("src","").addClass('hide');
            $logo.val("");
            $(this).addClass('hide');
        });

        $(".addlogo[data-toggle='selectimg']").on("callback",function(){
        	var $this=$(this);
        	$this.addClass('hide');
        	$("button.cancleLogo").removeClass('hide');
        });

		$("#select-modal").length && $("#select-modal").bind("init",function($modal){
			var $select2 = $('[data-toggle="select2"]',document);
			
		    $select2.length > 0 && require.async(['select2'], function() {
		        $select2.each(function() {
		            var t = $(this),
		                n = {},
		                c = t.data('maxcount'),
		                o = t.data('chang'),
		                r = t.data('remote') || t.attr('href'),
		                required = t.data("ruleRequired"),
		                required_msg = t.data("msgRequired")||t.data("placeholder");
		            c && (n.maximumSelectionSize = c);
		            if (required) {
		                var form=$(this).closest('form');
		                $(form).on("click", 'button[type="submit"]', function() {
		                    var val=t.val();
		                  if (!val) {
		                     config.msg.info(required_msg||"请输入必填项");
		                     return false;
		                 }
		                   
		                }) 
		            };

		            if (o) {
		                switch (o) {
		                    case "submit":
		                        {
		                            t.on("change", function(e) {
		                                t.closest("form ").submit();
		                            })
		                        }
		                }
		            };
		            t.select2(n)
		        })
		    });
		})
 });